package com.qz;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import cn.hutool.core.date.DateUtil;

import java.util.HashMap;
import java.util.Map;

public class BindDeviceExample {

    public static void main(String[] args) {
        // 设置请求的公共参数
        String appSecret = "your_app_secret";
        String appKey = "your_app_key";
        long timestamp = System.currentTimeMillis() / 1000; // 获取秒级时间戳
        
        // 设置接口 URL
        String url = "https://your_api_base_url/openapi/v2/bindDevice";
        
        // 创建请求体对象
        BindDeviceRequest request = new BindDeviceRequest("川A0001 挂", "川A0001", "34A00001", "15100000000");
        
        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("appsecret", appSecret);
        headers.put("appkey", appKey);
        headers.put("timestamp", String.valueOf(timestamp));
        
        // 发送 POST 请求并获取响应
        String response = HttpRequest.post(url)
            .addHeaders(headers)
            .body(JSONUtil.toJsonStr(request))  // 将请求对象转换为 JSON 字符串
            .execute()
            .body();
        
        // 打印响应结果
        System.out.println("Response: " + response);
        
        // 解析响应结果（假设响应符合示例格式）
        try {
            Map<String, Object> responseMap = JSONUtil.toBean(response, Map.class);
            if ("200".equals(responseMap.get("code"))) {
                System.out.println("绑定成功！");
                Map<String, Object> data = (Map<String, Object>) responseMap.get("data");
                System.out.println("挂车车牌号: " + data.get("hangPlate"));
                System.out.println("主车车牌号: " + data.get("carPlate"));
                System.out.println("设备号: " + data.get("deviceNo"));
                System.out.println("绑定状态: " + data.get("bindStatus"));
                System.out.println("绑定时间: " + DateUtil.date(Long.parseLong(data.get("createTime").toString() + "000")).toString());
            } else {
                System.out.println("接口错误: " + responseMap.get("message"));
            }
        } catch (Exception e) {
            System.out.println("解析响应失败: " + e.getMessage());
        }
    }
}
